/*
 * Filename:    DocumentServiceImpl.java
 * Copyright:   2014 中国平安保险（集团）股份有限公司 版权所有
 * Company:     平安科技养老险及健康险系统开发部
 * Create at:   2014-11-10
 */
package com.paic.mhis.hcpms.common.document.biz.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.paic.mhis.hcpms.common.document.biz.service.IDocumentService;
import com.paic.mhis.hcpms.common.document.dto.DocContentDTO;
import com.paic.mhis.hcpms.common.document.dto.DocParameterCondition;
import com.paic.mhis.hcpms.common.document.dto.DocSaveParamterDTO;
import com.paic.mhis.hcpms.common.document.dto.DocumentDTO;
import com.paic.mhis.hcpms.common.document.intergration.DocumentDao;
import com.paic.mhis.hcpms.common.document.util.Constant;
import com.pingan.um.client.util.UUID;

@Component("iDocumentService")
public class DocumentServiceImpl implements IDocumentService {
	@Autowired
	private DocumentDao documentDao;

	/**
	 * 更新文档内容
	 * 
	 * @param bean
	 */
	@Transactional
	public void updateDocContentDTO(DocSaveParamterDTO bean) {
		String taskNo = bean.getTaskNo();
		String taskType = bean.getTaskType();
		String docType = bean.getDocType();
		Map map = bean.getMap();
		String updateUser = bean.getUpdateUser();

		DocParameterCondition parameter = new DocParameterCondition();

		parameter.setTaskNo(taskNo);
		parameter.setTaskType(taskType);
		parameter.setDocType(docType);

		List<DocContentDTO> list = this.queryByDocParameterCondition(parameter);

		if (list != null && list.size() > 0) {

			DocContentDTO content = list.get(0);
			String docNo = content.getDocNo();

			for (Object keyObj : map.keySet()) {

				String key = keyObj.toString();

				String value = (String) map.get(key);

				DocContentDTO updateBean = new DocContentDTO();
				updateBean.setUpdateDate(new Date());
				updateBean.setUpdateUser(updateUser);
				updateBean.setDocKey(key);
				updateBean.setDocValue1(value);
				updateBean.setDocNo(docNo);

				this.documentDao.updateDocContentDTO(updateBean);

			}

		}

	}

	/**
	 * 查询文档内容
	 */
	public List<DocContentDTO> queryByDocParameterCondition(DocParameterCondition parameter) {

		List<DocContentDTO> list = this.documentDao.queryByDocParameterCondition(parameter);
		return list;
	}

	/**
	 * 保存文档
	 */
	@Transactional
	private void saveDocContentDTO(Map map, String creatUser, String docNo) {

		for (Object key : map.keySet()) {

			String keyStr = key.toString();
			String keyValue = (String) map.get(keyStr);

			DocContentDTO bean = new DocContentDTO();
			bean.setCreateDate(new Date());
			bean.setCreateUser(creatUser);
			bean.setUpdateDate(new Date());
			bean.setUpdateUser(creatUser);
			bean.setDocKey(keyStr);
			bean.setDocSeq("");
			bean.setDocNo(docNo);
			bean.setDocValue1(keyValue);
			bean.setIsValid(Constant.isValid_0);
			this.documentDao.saveDocContentDTO(bean);

		}

	}

	/**
	 * 保存文档
	 */
	@Transactional
	public void saveDocumentContent(DocSaveParamterDTO bean) {
		String docType = bean.getDocType();
		String taskType = bean.getTaskType();
		String taskNo = bean.getTaskNo();
		String createUser = bean.getCreateUser();

		DocParameterCondition parameter = new DocParameterCondition();
		parameter.setDocType(docType);
		parameter.setTaskType(taskType);
		parameter.setTaskNo(taskNo);

		List<DocumentDTO> list = this.documentDao.queryDocumentDTO(parameter);

		if (list != null && list.size() > 0) { // 说明存在了

			DocumentDTO documentBean = list.get(0);

			Map<String, Object> map = bean.getMap();

			this.saveDocContentDTO(map, createUser, documentBean.getDocNo());

		} else {

			Map<String, Object> map = bean.getMap();

			String docNo = UUID.getID();

			DocumentDTO docBean = new DocumentDTO();
			docBean.setDocNo(docNo);
			docBean.setDocType(docType);
			docBean.setTaskNo(taskNo);
			docBean.setTaskType(taskType);
			docBean.setCreateDate(new Date());
			docBean.setCreatedBy(createUser);
			docBean.setIsValid(Constant.isValid_0);
			docBean.setUpdateBy(createUser);
			docBean.setUpdateDate(new Date());

			this.documentDao.saveDocumentDTO(docBean);

			this.saveDocContentDTO(map, createUser, docNo);

		}

	}

}
